import 'dart:async';

FutureOr<T> retry<T>(
  FutureOr<T> Function() future, {
  int retryCount = 2,
  Duration delay = const Duration(milliseconds: 100),
}) async {
  try {
    return await future();
  } catch (e) {
    if (retryCount == 0) {
      throw e;
    } else {
      print("retry:$retryCount,${future}");
      await Future.delayed(delay);
      return retry(future, retryCount: retryCount - 1, delay: delay);
    }
  }
}

void main(List<String> args) async {
  int i = 0;
  Future<int> run(int b) async {
    i++;
    print("i:$i");
    if (i < 2) {
      throw Exception("");
    } else {
      return i;
    }
  }

  var value = await retry(() async {
    return await run(2);
  }, retryCount: 2);
  print("value:$value");
}
